package com.hmall.item.web;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hmall.common.dto.PageDTO;
import com.hmall.item.pojo.Item;
import com.hmall.item.service.IItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("item")
public class ItemController {

    @Autowired
    private IItemService itemService;

    /**
     * 查询全部
     * @param page
     * @param size
     * @return
     */
    @GetMapping("list")
    public PageDTO getList(@RequestParam("page")Integer page ,@RequestParam("size")Integer size) {
        //分页查询
        Page<Item> result = itemService.page(new Page<>(page, size));
       //返回值
        return  new PageDTO<>(result.getTotal(),result.getRecords());
    }

    /**
     * 根据id查询
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Item getById(@PathVariable("id")Long id) {
        return itemService.getById(id);
    }

    /**
     * 新增接口
     * @param item
     */
    @PostMapping
    public void save(@RequestBody Item item) {
        itemService.save(item);
    }

    /**
     * 下架商品
     * @param id
     * @param status
     */
    @PutMapping("/status/{id}/{status}")
    public void updateItemStatus(@PathVariable("id") Long id, @PathVariable("status") Integer status){
        itemService.updateStatus(id, status);
    }
    /**
     * 修改商品
     * @param item
     */
    @PutMapping()
    public void updateByItem(@RequestBody Item item) {

        itemService.updateById(item);
    }


    /**
     * 删除商品
     * @param id
     */
    @DeleteMapping("/{id}")
    public void DeleteById(@PathVariable("id") Long id) {

        itemService.removeById(id);
    }

    /**
     * 扣减库存
     * @param itemId
     * @param num
     */
    @DeleteMapping("/stock/{itemId}/{num}")
    public void DeleteById(@PathVariable("itemId") Long itemId,@PathVariable("num") Integer num) {
        itemService.deductStock(itemId,num);
    }



}
